package com.fish.yssb.ui.page.activity.other;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.fish.yssb.Consts;
import com.fish.yssb.R;
import com.fish.yssb.api.retData;
import com.fish.yssb.api.setApi;
import com.fish.yssb.app.BaseApp;
import com.fish.yssb.ui.page.activity.base.BaseActivity;
import com.fish.yssb.ui.page.util.TopToastUtil;
import com.jaeger.library.StatusBarUtil;
import com.syd.oden.circleprogressdialog.core.CircleProgressDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * A login screen that offers login via email/password.
 */
public class SyncTimeActivity extends BaseActivity implements OnClickListener {
    private static final String TAG = SyncTimeActivity.class.getSimpleName();

    private TextView btn_1,btn_2;
    private LinearLayout back;
    private TimePickerView pvTime;
    private LinearLayout ll_curtime,ll_chantime;
    private ImageView ck_curtime,ck_seltime;
    private TextView tv_time;
    private CircleProgressDialog circleProgressDialog
            ;

    public static void start(Context context) {
        Intent intent = new Intent(context, SyncTimeActivity.class);
        context.startActivity(intent);
    }

    @Override
    public  void onDestroy() {
        super.onDestroy();

        EventBus.getDefault().unregister( this );
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setDarkMode(this);
        setContentView(R.layout.activity_synctime);
        initview();
        circleProgressDialog = new CircleProgressDialog(SyncTimeActivity.this);
        circleProgressDialog.setText("命令执行中");
        EventBus.getDefault().register( this );
    }

    @Subscribe
    public void onEvent(final retData result) {
        //接收以及处理数据
        runOnUiThread(new Runnable() {
            public void run() {
                int strType = result.getType();
                int ret = result.getRet();
                int status = result.getStatus();

                if(strType == Consts.cm_systime)
                {
                    circleProgressDialog.dismiss();
                    if(ret == Consts.cm_timeout) {
                        TopToastUtil.getInstance(SyncTimeActivity.this, R.layout.showdialog).showShortToast("命令执行超时");
                    }

                    if(ret == Consts.cm_end) {
                        if(status == 1) {
                            TopToastUtil.getInstance(SyncTimeActivity.this, R.layout.showdialog).showShortToast("时间同步执行成功");
                        }else{
                            TopToastUtil.getInstance(SyncTimeActivity.this, R.layout.showdialog).showShortToast("时间同步执行失败");

                        }
                    }
                }

            }
        });




    };


    private boolean flag=true;
    //初始化视图
    @SuppressLint("WrongViewCast")
    private void initview() {
        //登录层、下拉层、其它登录方式层

        back = findViewById(R.id.tv_back);
        back.setOnClickListener(this);

        ll_curtime = findViewById(R.id.ll_curtime);
        ll_chantime = findViewById(R.id.ll_chantime);
        ck_curtime = findViewById(R.id.ck_curtime);
        ck_seltime = findViewById(R.id.ck_seltime);
        tv_time = findViewById(R.id.tv_time);

        initTimePicker();
        findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Date  curDate = new Date();;
                if(!flag)
                {
                    if(sendDate == null)
                    {
                        Toast.makeText(SyncTimeActivity.this, "请选择自定义时间.",
                                Toast.LENGTH_SHORT).show();
                        return;
                    }
                    curDate = sendDate;
                }
                int ret =  setApi.getInstance(SyncTimeActivity.this).sysTime(new Date());
                if(ret ==1)
                {
                    circleProgressDialog.showDialog();
                }else{
                    TopToastUtil.getInstance(SyncTimeActivity.this,R.layout.showdialog).showShortToast("发送时间同步失败");

                }

            }
        });
        findViewById(R.id.btn_chancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        ll_curtime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                flag = true;
                ck_curtime.setImageDrawable(getResources().getDrawable(R.drawable.c_synctime_u));
                ck_seltime.setImageDrawable(getResources().getDrawable(R.drawable.c_synctime));
            }
        });
        ll_chantime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                flag = false;
                ck_curtime.setImageDrawable(getResources().getDrawable(R.drawable.c_synctime));
                ck_seltime.setImageDrawable(getResources().getDrawable(R.drawable.c_synctime_u));
            }
        });

        tv_time.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                pvTime.show(v);
            }
        });

    }

    @Override
    public void onClick(View view) {


        switch (view.getId()) {

            case R.id.tv_back:
                finish();
                break;

            default:
                break;
        }


    }


    private Date  sendDate = null;
    private void initTimePicker() {//Dialog 模式下，在底部弹出
        pvTime = new TimePickerBuilder(SyncTimeActivity.this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
//                Toast.makeText(context, getTime(date), Toast.LENGTH_SHORT).show();
                Log.i("pvTime", "onTimeSelect");
//                TopToastUtil.getInstance(getActivity(),R.layout.showdialog).showShortToast("时间同步成功");
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String dateString = formatter.format(date);
                tv_time.setText(dateString);
                sendDate = date;
            }
        })
                .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                    @Override
                    public void onTimeSelectChanged(Date date) {
                        Log.i("pvTime", "onTimeSelectChanged");

                    }
                })
                .setTitleText("查询设置")
                .setType(new boolean[]{true, true, true, true, true, true})
                .isDialog(true) //默认设置false ，内部实现将DecorView 作为它的父控件。
                .addOnCancelClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Log.i("pvTime", "onCancelClickListener");
                    }
                })
                .setItemVisibleCount(5) //若设置偶数，实际值会加1（比如设置6，则最大可见条目为7）
                .setLineSpacingMultiplier(2.0f)
                .isAlphaGradient(true)
                .build();

        Dialog mDialog = pvTime.getDialog();
        if (mDialog != null) {

            FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    Gravity.BOTTOM);

            params.leftMargin = 0;
            params.rightMargin = 0;
            pvTime.getDialogContainerLayout().setLayoutParams(params);

            Window dialogWindow = mDialog.getWindow();
            if (dialogWindow != null) {
                dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式
                dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示
                dialogWindow.setDimAmount(0.3f);
            }
        }
    }

}

